package com.braveman.im.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.braveman.im.R;
import com.braveman.im.bean.FriendshipListBean;
import com.braveman.im.bean.GetMineFriendsBean;
import com.braveman.im.manager.ApiManager;
import com.ht.baselibs.core.EventCenter;
import com.ht.baselibs.core.EventCode;
import com.ht.baselibs.retrofit.NetCallBack;
import com.ht.baselibs.ui.base.BaseFragment;
import com.ht.baselibs.utils.ActivityUtils;
import com.ht.baselibs.utils.IDCheckUtil;
import com.hyphenate.easeui.domain.EaseUser;
import com.hyphenate.easeui.widget.EaseContactList;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;
import retrofit2.Call;
import retrofit2.Response;

public class FriendsForMingPianFragment extends BaseFragment {
    private EaseContactList contactListLayout;
    private ListView listView;
    private List<EaseUser> contactList = new ArrayList<>();
    private List<GetMineFriendsBean> memberlist = new ArrayList<>();

    @Override
    protected boolean isStaticPage() {
        return true;
    }

    @Override
    protected View initContentView() {
        return LayoutInflater.from(mContext).inflate(R.layout.frag_friends_fragment, null);
    }

    @Override
    protected void initChildView() {

    }

    @Override
    protected void load() {
        ApiManager.getApi().getMineFriends().enqueue(new NetCallBack<List<GetMineFriendsBean>>() {


            @Override
            protected void onSuccess(Call<List<GetMineFriendsBean>> call, Response<List<GetMineFriendsBean>> response) {
                if (response != null) {
                    memberlist.clear();
                    memberlist = response.body();
                    if (memberlist != null) {
                        for (GetMineFriendsBean friendsInfo : memberlist) {
                            for (FriendshipListBean friendsList : friendsInfo.friendshipList) {
                                EaseUser easeUser = new EaseUser(friendsList.memberName);
                                easeUser.setAvatar(friendsList.friendAvatar);
                                easeUser.setInitialLetter(friendsInfo.name);
                                contactList.add(easeUser);
                            }

                        }
                        contactListLayout = (EaseContactList) getView().findViewById(R.id.contact_list);
                        contactListLayout.init(contactList);
                        contactListLayout.refresh();
                        listView = contactListLayout.getListView();
                        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                                Intent intent = new Intent();
                                intent.putExtra("minpianInfo", (Serializable) memberlist.get(i).friendshipList);
                                EventBus.getDefault().post(new EventCenter(EventCode.EVENT_SET_RESULT, intent));
                            }
                        });
                    }


                }

            }
        });
    }


    @Override
    protected void setChildViewListener() {

    }


    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
        }
    }

}
